package com.dbq.fast.business.me;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dbq.fast.App;
import com.dbq.fast.R;
import com.dbq.fast.business.login.api.NewLoginApi;
import com.dbq.fast.business.main.dialog.BindEmailDialog;
import com.dbq.fast.common.base.BaseActivity;
import com.dbq.fast.common.bean.User;
import com.dbq.fast.common.net.RemoteApiCallBack;
import com.dbq.fast.util.SpUtil;
import com.dbq.fast.util.SystemUtil;
import com.dbq.fast.util.WindowToastUtils;
import com.yiba.vpn.utils.Constants;
import com.yiba.vpn.utils.RxBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.processors.FlowableProcessor;
import io.reactivex.schedulers.Schedulers;

import static com.dbq.fast.business.login.NewLoginActivity.HAS_LOGIN_SUCCESS;

public class AccountManagerActivity extends BaseActivity{
    @BindView(R.id.tv_id)
    TextView tv_id;
    @BindView(R.id.tv_phone)
    TextView tv_phone;
    @BindView(R.id.tv_email)
    TextView tv_email;
    Unbinder bind;
    @BindView(R.id.tv_email_isempty)
    TextView tv_email_isempty;

    @BindView(R.id.navi_bar)
    RelativeLayout navi_bar;
    private FlowableProcessor<String> bindEmailListener;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.accountmanager);
        bind = ButterKnife.bind(this);
        int statusBarHeight = SystemUtil.getStatusBarHeight(this);
        if(statusBarHeight>0){
            RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) navi_bar.getLayoutParams();
            lp.setMargins(0,statusBarHeight,0,0);
            navi_bar.setLayoutParams(lp);
        }
        bindEmailListener = RxBus.get().register(Constants.RXBUS_BIND_EAMIL, String.class);
        bindEmailListener.onBackpressureLatest()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(s -> initView());
        initView();
    }

    private void initView() {
        User.getInstance().init();
        if(User.getInstance()!=null) {
            if (tv_id != null) {
                tv_id.setText(User.getInstance().getVpnId());
            }
            if (tv_phone != null) {
                tv_phone.setText(User.getInstance().getAccount());
            }
            if (TextUtils.isEmpty(User.getInstance().getEmail())) {
                if (tv_email_isempty != null && tv_email != null) {
                    tv_email_isempty.setVisibility(View.VISIBLE);
                    tv_email.setVisibility(View.GONE);
                }
            } else {
                if (tv_email_isempty != null && tv_email != null) {
                    tv_email.setText(User.getInstance().getEmail());
                    tv_email.setVisibility(View.VISIBLE);
                    tv_email_isempty.setVisibility(View.GONE);
                }
            }
        }
    }

    @OnClick({R.id.tv_email_isempty,R.id.back, R.id.logout})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.tv_email_isempty:
                showBindEmailDialog();
                break;
            case R.id.back:
                finish();
                break;
            case R.id.logout:
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle(R.string.tip)
                        .setMessage(R.string.tip_logout_confirm)
                        .setNegativeButton(android.R.string.cancel, (dialogInterface, i) -> {

                        })
                        .setPositiveButton(android.R.string.ok, (dialogInterface, i) -> {
                            logout();
                        }).create().show();
                break;
        }
    }

    private void logout() {
        NewLoginApi.logOut(new RemoteApiCallBack<String>() {
            @Override
            public void onFail(int status, String message) {
                WindowToastUtils.showShort(App.getContext(), message);
            }

            @Override
            public void onSuccess(String s) {
                WindowToastUtils.showShort(App.getContext(), getString(R.string.logout_succ), R.color.green_succ);
                SpUtil.setBoolean(HAS_LOGIN_SUCCESS, false);
                RxBus.get().post(Constants.RXBUS_LOGIN_OUT, "loginOut");
                RxBus.get().post(Constants.RXBUS_START_VPN, Constants.RXBUS_LOGOUT);
            }
        });
    }

    /***绑定email**/
    private void showBindEmailDialog() {
        BindEmailDialog.show(getSupportFragmentManager());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        bind.unbind();
    }
}
